<?php
// api/db.php

$host = '127.0.0.1';       // Or your database host
$db   = 'root'; // Your database name <<< --- MAKE SURE THIS IS CORRECT
$user = 'root';            // Your database username <<< --- MAKE SURE THIS IS CORRECT
$pass = '874604';                // Your database password <<< --- MAKE SURE THIS IS CORRECT
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION, // Throw exceptions on errors
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,       // Fetch associative arrays
    PDO::ATTR_EMULATE_PREPARES   => false,                  // Use native prepared statements
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    // Don't echo detailed errors in production, log them instead
    http_response_code(500);
    // Ensure JSON header even for critical errors if possible
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Database connection failed. Please check server configuration.']);
    // Log the detailed error: error_log("Database Connection Error: " . $e->getMessage());
    exit; // Stop script execution
}
?>